package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.BusAppointment;
import cn.wolfcode.business.domain.BusStatement;
import cn.wolfcode.business.mapper.BusAppointmentMapper;
import cn.wolfcode.business.mapper.BusStatementMapper;
import cn.wolfcode.business.service.IBusAppointmentService;
import cn.wolfcode.business.util.RegexUtils;
import cn.wolfcode.business.util.VehiclePlateNoUtil;
import cn.wolfcode.common.exception.ServiceException;
import cn.wolfcode.common.utils.DateUtils;

import cn.wolfcode.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 养修信息预约Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-28
 */
@Service
@Transactional
public class BusAppointmentServiceImpl implements IBusAppointmentService {
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;
    @Autowired
    private BusStatementMapper busStatementMapper;

    /**
     * 查询养修信息预约
     *
     * @param id 养修信息预约主键
     * @return 养修信息预约
     */
    @Override
    public BusAppointment selectBusAppointmentById(Long id) {
        return busAppointmentMapper.selectBusAppointmentById(id);
    }

    /**
     * 查询养修信息预约列表
     *
     * @param busAppointment 养修信息预约
     * @return 养修信息预约
     */
    @Override
    public List<BusAppointment> selectBusAppointmentList(BusAppointment busAppointment) {
        return busAppointmentMapper.selectBusAppointmentList(busAppointment);
    }

    /**
     * 新增养修信息预约
     *
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int insertBusAppointment(BusAppointment busAppointment) {
        isParamAcess(busAppointment);

        int count = busAppointmentMapper.seleteTodayCount(busAppointment.getCustomerPhone());
        if (count>=3){
            throw new ServiceException("同一手机号一天不能预约超过三单");
        }

        BusAppointment busAppointment1 = new BusAppointment();
        BeanUtils.copyProperties(busAppointment, busAppointment1);

        busAppointment1.setCreateTime(DateUtils.getNowDate());
        return busAppointmentMapper.insertBusAppointment(busAppointment1);
    }

    public void isParamAcess(BusAppointment busAppointment) {
        Assert.notNull(busAppointment, "参数不能为 null");

        boolean phoneLegal = RegexUtils.isPhoneLegal(busAppointment.getCustomerPhone());
        Assert.isTrue(phoneLegal, "非法手机号码");

        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(busAppointment.getLicensePlate());
        Assert.notNull(vehiclePlateNo, "非法车牌号");

//        boolean timeFlag = busAppointment.getAppointmentTime().before(DateUtils.getNowDate());
//        Assert.isTrue(!timeFlag, "预约时间不能比当前时间早");
    }

    /**
     * 修改养修信息预约
     *
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int updateBusAppointment(BusAppointment busAppointment) {
        isParamAcess(busAppointment);

        BusAppointment newObj = busAppointmentMapper.selectBusAppointmentById(busAppointment.getId());
        Assert.isTrue(BusAppointment.STATUS_APPOINTMENT.equals(newObj.getStatus()),"只有预约中状态才可以编辑");

        BusAppointment busAppointment1 = new BusAppointment();
        BeanUtils.copyProperties(busAppointment, busAppointment1);

        return busAppointmentMapper.updateBusAppointment(busAppointment1);
    }

    /**
     * 批量删除养修信息预约
     *
     * @param ids 需要删除的养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentByIds(Long[] ids) {
        return busAppointmentMapper.deleteBusAppointmentByIds(ids);
    }

    /**
     * 删除养修信息预约信息
     *
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentById(Long id) {
        return busAppointmentMapper.deleteBusAppointmentById(id);
    }

    @Override
    public int updateArrivalState(Long id) {
        Assert.notNull(id, "参数不能为Null");
        BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        if (busAppointment==null){
            throw new ServiceException("非法参数");
        }
        Integer status = busAppointment.getStatus();
        Assert.isTrue(BusAppointment.STATUS_APPOINTMENT.equals(status), "预约中的订单才能进行到店操作");
        return busAppointmentMapper.updateArrivalState(id, BusAppointment.STATUS_ARRIVAL);
    }

    @Override
    public int cancelBusAppointment(Long id) {
        Assert.notNull(id, "参数不能为Null");

        BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        if (busAppointment==null){
            throw new ServiceException("非法参数");
        }
        Integer status = busAppointment.getStatus();
        if (!(BusAppointment.STATUS_APPOINTMENT.equals(status)||BusAppointment.STATUS_ARRIVAL.equals(status)||BusAppointment.STATUS_SETTLE.equals(status))){
            throw new ServiceException("当前状态无法取消");
        }
//        busAppointment.setStatus(BusAppointment.STATUS_CANCEL);
//        return busAppointmentMapper.updateBusAppointment(busAppointment);
        return busAppointmentMapper.updateStatus(id,BusAppointment.STATUS_CANCEL);
    }

    @Override
    public int updateDel(Long[] ids) {
        if (ids.length == 0) {
            throw new ServiceException("非法参数");
        }

        List<Integer> status = new ArrayList<>();
        status.add(BusAppointment.STATUS_APPOINTMENT);
        status.add(BusAppointment.STATUS_CANCEL);
        status.add(BusAppointment.STATUS_OVERTIME);

        int count = busAppointmentMapper.updateDel(ids,status);
        if (ids.length != count){
            throw new ServiceException("所选项有不能删除的项");
        }

        return count;
    }

    /**
     * 生成或跳转结算单明细
     *
     * @param appointmentId 养修信息预约主键
     * @return 结果
     */
    @Override
    public BusStatement generate(Long appointmentId) {
        BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(appointmentId);
        if (busAppointment==null) {
            throw new ServiceException("非法参数");
        }
        BusStatement busStatement = busStatementMapper.selectByAppointmentId(appointmentId);
        if (busStatement==null) {
            busStatement = new BusStatement();
            BeanUtils.copyProperties(busAppointment,busStatement);
            busStatement.setId(null);
            busStatement.setStatus(BusStatement.STATUS_CONSUME);
            busStatement.setAppointmentId(appointmentId);
            busStatementMapper.insertBusStatement(busStatement);

            busAppointment.setStatus(BusAppointment.STATUS_SETTLE);
            busAppointmentMapper.updateBusAppointment(busAppointment);
        }
        return busStatement;
    }
}
